package kz.pro.client;

import java.util.List;

import kz.pro.client.form.AddForm;
import kz.pro.client.form.UpdateForm;
import kz.pro.shared.entity.UserEntity;
import kz.pro.shared.services.UserService;
import kz.pro.shared.services.UserServiceAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MyTest implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private final UserServiceAsync userService = GWT
			.create(UserService.class);
	
	private List<UserEntity> list;
	private UserEntity currentUser;
	public void onModuleLoad() {
		final Button saveButton = new Button("+");
		final Button removeButton = new Button("-");
		final Button updateButton = new Button("...");
		final CellTable<UserEntity> userContact = new CellTable<UserEntity>();
		final VerticalPanel verticalPanel = new VerticalPanel();
		saveButton.setSize("25px", "30px");
		removeButton.setSize("25px", "30px");
		updateButton.setSize("25px", "30px");
		verticalPanel.add(saveButton);
		verticalPanel.setCellHeight(saveButton, "5px");
		verticalPanel.add(removeButton);
		verticalPanel.add(updateButton);
		saveButton.addStyleName("sendButton");
		removeButton.addStyleName("sendButton");
		updateButton.addStyleName("sendButton");
		TextColumn<UserEntity> userName = new TextColumn<UserEntity>() {	
			@Override
			public String getValue(UserEntity userEntity) {

				return userEntity.getName();
			}
			
		};
		
		
		TextColumn<UserEntity> userSurname = new TextColumn<UserEntity>() {

			@Override
			public String getValue(UserEntity userEntity) {
			
				return userEntity.getSurname();
			}
		};
		
		TextColumn<UserEntity> userPhone = new TextColumn<UserEntity>() {
			
			@Override
			public String getValue(UserEntity userEntity) {
			
				return userEntity.getPhoneNumber();
			}
		};
		
		TextColumn<UserEntity> userPhoneType = new TextColumn<UserEntity>() {

			@Override
			public String getValue(UserEntity userEntity) {
				
				return userEntity.getPhoneType();
			}
		};
		
		userContact.addColumn(userName, "Name");
		userContact.addColumn(userSurname, "Surname");
		userContact.addColumn(userPhone, "Phone number");
		userContact.addColumn(userPhoneType, "Phone type");
		
		ListDataProvider<UserEntity> dataProvider = new ListDataProvider<UserEntity>();
		dataProvider.addDataDisplay(userContact);
		list = dataProvider.getList();
		updateTable();
		saveButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				AddForm userForm = new AddForm();
				userForm.center();
			}
		});
		
		final SingleSelectionModel<UserEntity> selectionModel = new SingleSelectionModel<UserEntity>();
		userContact.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
		      public void onSelectionChange(SelectionChangeEvent event) {
		          UserEntity userEnity = selectionModel.getSelectedObject();
		          if (userEnity != null) {
		            currentUser = userEnity;
		          }
		        }
		      });
		
		
		updateButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(currentUser!=null){
					UpdateForm updateForm = new UpdateForm(currentUser);
					updateForm.center();
				}
			}
		});
		RootPanel.get("userContact").add(userContact);
		RootPanel.get("buttons").add(verticalPanel);
		
		removeButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(currentUser!=null){
					userService.deleteUser(currentUser, new AsyncCallback<Void>() {
	
						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}
	
						@Override
						public void onSuccess(Void result) {
							Window.Location.reload();
							
						}
					});
					
				}
			}
		});
	}
	
	public void updateTable(){
		userService.userList(new AsyncCallback<List<UserEntity>>() {
		
			@Override
		    public void onSuccess(List<UserEntity> users) {
				list.clear();
				for (UserEntity contact : users) {
				      list.add(contact);
				    }
		    }

		    @Override
		    public void onFailure(Throwable caught) {
		        Window.alert("ups...");
		    }
		});

	}
}
